package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopPhotoClass;
import com.myapp.ruyixing.model.ShopPhotoClass.ShopPhotoClassItem;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class SelectPhotoClassActivity extends BaseActivity implements
		OnItemClickListener {

	private ListView mListView;
	private TextView mEmptyView;

	private String mParentTypeId;
	private ShopPhotoClass mShopPhotoClass;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ShopPhotoClass data = (ShopPhotoClass) o;
			mShopPhotoClass = data;
			
			if (data != null && data.data != null) {
				MyListAdapter adapter = new MyListAdapter(data.data);
				mListView.setAdapter(adapter);
			}
			mListView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_select_photo_class);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);

		String mShopId = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_ID);
		mParentTypeId = getIntent().getStringExtra("typeid");
		if (TextUtils.isEmpty(mShopId) == true) {
			Toast.makeText(getApplicationContext(), "商户为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			RYHttpClient.getPhotoClass(this, mHandler, mShopId, mParentTypeId);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ShopPhotoClassItem o = (ShopPhotoClassItem) mListView.getAdapter()
				.getItem(position);
		
		complete(o.id, o.name);
	}
	
	private void complete(String typeid, String name) {
		if (mShopPhotoClass != null && mShopPhotoClass.data != null) {
			for (ShopPhotoClassItem item : mShopPhotoClass.data) {
				if (name.equals(item.name)) {
					typeid = item.id;
				}
			}
		}
		
		Intent data = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("id", typeid);
		bundle.putString("name", name);
		data.putExtras(bundle);
		
		setResult(RESULT_OK, data);
		finish();
	}

	private void initView() {

		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(this);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		
		final EditText search = (EditText) findViewById(R.id.edit_search);
		search.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				String s = search.getText().toString();
				s = s.trim();
				if (!TextUtils.isEmpty(s)) {
					complete(null, s);
				}
				return false;
			}
		});
	}

	private class MyListAdapter extends ArrayAdapter<ShopPhotoClassItem> {

		public MyListAdapter(List<ShopPhotoClassItem> list) {
			super(SelectPhotoClassActivity.this, -1, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_shop_photo_select, parent, false);
			}

			ShopPhotoClassItem item = getItem(position);
			((TextView) convertView).setText(item.name);

			return convertView;

		}
	}
}
